package com.humble.vichatbackend.interceptor;

import com.humble.vichatbackend.model.enums.ErrorCode;
import com.humble.vichatbackend.constant.UserConstant;
import com.humble.vichatbackend.exception.BusinessException;
import com.humble.vichatbackend.model.vo.UserVO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import jakarta.servlet.http.HttpServletRequest;


@Slf4j
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String contextPath = request.getContextPath();
        log.info(contextPath);
        UserVO currentUser = (UserVO)request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        if (currentUser == null) {
            log.error("请求拦截，用户未登录！");
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        return true;
    }
}
